<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="会员名称" prop="customerName">
        <el-input v-model="dataForm.customerName" placeholder="会员名称" disabled>
          <el-button slot="append" @click="openCustomer">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="会员卡号" prop="customerCard">
        <el-input v-model="dataForm.customerCard" placeholder="会员卡号" disabled></el-input>
      </el-form-item>
      <el-form-item label="会员原有余额">
        <el-input v-model="dataForm.originalMoney" placeholder="会员原有余额" disabled></el-input>
      </el-form-item>
      <el-form-item label="金额" prop="money">
        <el-input v-model="dataForm.money" placeholder="金额"></el-input>
      </el-form-item>
      <el-form-item label="充值部门">
        <el-input v-model="dataForm.deptName" placeholder="充值部门" disabled></el-input>
      </el-form-item>
      <el-form-item label="充值人">
        <el-input v-model="dataForm.operationUserName" placeholder="充值人" disabled></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="isCheck">确定</el-button>
    </span>
    <!-- 弹窗，查询顾客 -->
    <search-customer v-if="searchCustomerVisible" ref="searchCustomer" @refreshForm="getCustomerData"></search-customer>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getObj, addObj, putObj } from '@/api/eims/customerbalance'
  
  import SearchCustomer from '@/components/business/customer-search' // 查询顾客

  export default {
    data() {
      return {
        visible: false,
        isCheck: false,
        searchCustomerVisible: false,
        dataForm: {
          id: 0,
          customerId: '',
          customerCard: '',
          originalMoney: '',
          money: '',
          currentMoney: '',
          changeReason: '',
          changeBillId: '',
          operationUser: '',
          deptId: '',
          unitId: '',
          tenantId: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: ''
        },
        dataRule: {
          customerCard: [{required: true,message: '会员卡号不能为空',trigger: 'blur'}],
          originalMoney: [{required: true,message: '原有金额不能为空',trigger: 'blur'}],
          money: [{required: true,message: '金额不能为空',trigger: 'blur'}],
          operationUser: [{required: true,message: '操作人员/充值人员不能为空',trigger: 'blur'}]
        }
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    components: {
      SearchCustomer
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        if (this.dataForm.id == 0) {
          this.dataForm.operationUser = this.userInfo.id
          this.dataForm.operationUserName = this.userInfo.realName
          this.dataForm.deptId = this.userInfo.lessDeptId
          this.dataForm.deptName = this.userInfo.lessDeptName
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.isCheck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.isCheck = false
              }).catch(() => {
                this.isCheck = false
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.isCheck = false
              }).catch(() => {
                this.isCheck = false
              })
            }
          }else{
            this.isCheck = false
          }
        })
      },
      openCustomer() {
        this.searchCustomerVisible = true
        this.$nextTick(() => {
          this.$refs.searchCustomer.init()
        })
      },
      // 获取顾客信息
      getCustomerData(data) {
        this.dataForm.originalMoney = data.moneyBalance || 0
        this.dataForm.customerId = data.id
        this.dataForm.customerCard = data.customerCard
        this.dataForm.customerName = data.name
      },
    }
  }
</script>
